package com.fatcat.easybuy.controller;

import com.fatcat.easybuy.annotaion.CheckPermission;
import com.fatcat.easybuy.pojo.EasyBuyUserAddress;
import com.fatcat.easybuy.service.EasyBuyUserAddressService;
import com.fatcat.easybuy.service.EasyBuyUserService;
import com.fatcat.easybuy.util.ResponseResult;
import org.apache.tomcat.Jar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 李开辰
 * @since 2025/9/16 09:41
 */
@CheckPermission({"0", "1"})
@RestController
@RequestMapping("/address")
public class EasyBuyAddressController {
    @Autowired
    private EasyBuyUserAddressService easyBuyUserAddressService;
    @Autowired
    private EasyBuyUserService easyBuyUserService;

    @GetMapping("/getAllAddress")
    public ResponseResult getAllAddress(@RequestAttribute("userId") Long userId) {
        List<EasyBuyUserAddress> easyBuyUserAddressList = easyBuyUserAddressService.getAllAddressByUserid(userId);
        if (easyBuyUserAddressList.isEmpty()) {
            return ResponseResult.error("您还没有收货地址");
        }
        return ResponseResult.success().add("data", easyBuyUserAddressList);
    }

    @GetMapping("/setDefault/{id}")
    public ResponseResult setDefault(
            @PathVariable Integer id,
            @RequestAttribute("userId") Long userId) {

        if (easyBuyUserAddressService.setDefaultAddress(userId, id)) {
            return ResponseResult.success("设置默认地址成功");
        }
        return ResponseResult.error("设置默认地址失败");
    }

    @PostMapping("/updateAddress")
    public ResponseResult updateAddress(
            @RequestBody EasyBuyUserAddress easyBuyUserAddress,
            @RequestAttribute("userId") Long userId) {
        if (easyBuyUserAddressService.updateUserAddress(easyBuyUserAddress)) {
            return ResponseResult.success("修改地址成功");
        }
        return ResponseResult.error("修改地址失败");
    }

    @PostMapping("/addAddress")
    public ResponseResult addAddress(@RequestBody EasyBuyUserAddress easyBuyUserAddress,
                                     @RequestAttribute("userId") Long userId) {
        if (easyBuyUserAddressService.selectAddressCountOfUser(userId) >= 16) {
            return ResponseResult.error("您的地址数量已经抵达上限（16个），请删除部分地址后再添加");
        }
        easyBuyUserAddress.setUserId(userId);
        if (easyBuyUserAddressService.addUserAddress(easyBuyUserAddress)) {
            return ResponseResult.success("添加地址成功");
        }
        return ResponseResult.error("添加地址失败");
    }

    @DeleteMapping("/deleteAddress/{id}")
    public ResponseResult deleteAddress(@PathVariable Integer id, @RequestAttribute("userId") Long userId) {
        if (easyBuyUserAddressService.deleteUserAddress(id)) {
            return ResponseResult.success("删除地址成功");
        }
        return ResponseResult.error("删除地址失败");
    }

    @GetMapping("/checkAddressNum")
    public ResponseResult checkAddressNum(@RequestAttribute("userId") Long userId) {
        if (easyBuyUserAddressService.selectAddressCountOfUser(userId) >= 16) {
            return ResponseResult.error("您的地址数量已经抵达上限（16个），请删除部分地址后再添加");
        }
        return ResponseResult.success("邮箱数量通过");
    }
}
